<?php
namespace app\admin\controller;

use app\admin\exception\AdminException;
use app\admin\service\rbac\Users\Service;
use app\admin\model\ManageUser;
use think\Db;
use think\facade\Cache;
use think\facade\Env;
use think\facade\Session;
use think\Paginator;
use think\paginator\driver\Bootstrap;
use think\Request;

class Index extends Admin
{
    public function index() {
        $counts = [];
        $cache_time = 7200; // s

        // 用户数
        $counts['custom_total'] = model('custom/Custom')->cache($cache_time)->count();
        $counts['custom_today'] = model('custom/Custom')->whereTime('create_time','today')->count();
        $counts['custom_disable'] = model('custom/Custom')->cache($cache_time)->where('status',-1)->count();

        // 站点数
        $counts['site_total'] = model('site/Site')->cache($cache_time)->count();
        $counts['site_today'] = model('site/Site')->whereTime('create_time','today')->count();
        $counts['site_overdue'] = model('site/Site')->cache($cache_time)->whereTime('deadline','<=',time())->count();

        // 充值金额
        $counts['recharge_money_total'] = model('custom/CustomFee')->cache($cache_time)->where('type' , 'recharge')->sum('money');
        $counts['recharge_money_month'] = model('custom/CustomFee')->cache($cache_time)->where('type' , 'recharge')->whereTime('create_time','month')->sum('money');
        $counts['recharge_money_today'] = model('custom/CustomFee')->where('type' , 'recharge')->whereTime('create_time','today')->sum('money');

        // 充值笔数
        $counts['recharge_total'] = model('custom/CustomFee')->cache($cache_time)->where('type', 'recharge')->count();
        $counts['recharge_month'] = model('custom/CustomFee')->cache($cache_time)->where('type', 'recharge')->whereTime('create_time','month')->count();
        $counts['recharge_today'] = model('custom/CustomFee')->where('type', 'recharge')->whereTime('create_time','today')->count();

        // 消费金额
        $counts['deduction_money_total'] = model('custom/CustomFee')->cache($cache_time)->where('type' , 'deduction')->sum('money');
        $counts['deduction_money_month'] = model('custom/CustomFee')->cache($cache_time)->where('type' , 'deduction')->whereTime('create_time','month')->sum('money');
        $counts['deduction_money_today'] = model('custom/CustomFee')->where('type' , 'deduction')->whereTime('create_time','today')->sum('money');

        // 消费笔数
        $counts['deduction_total'] = model('custom/CustomFee')->cache($cache_time)->where('type', 'deduction')->count();
        $counts['deduction_month'] = model('custom/CustomFee')->cache($cache_time)->where('type', 'deduction')->whereTime('create_time','month')->count();
        $counts['deduction_today'] = model('custom/CustomFee')->where('type', 'deduction')->whereTime('create_time','today')->count();

        $this->assign('counts',$counts);
//        print_r($counts);die;



        return $this->render('index');
    }

    //修改密码
    public function updateInfo(Request $request)
    {
        if ($request->isPost()) {
            $validate = $this->validate($request->post(), '\app\admin\validate\ChangePassword');

            if ($validate !== true) {
                throw new AdminException($validate);
            }

            //判断原密码是否相等
            $model = new \app\admin\service\rbac\Users\Service();
            $user = ManageUser::where('id', $model->getManageId())->find();
            $oldPassword = $model->checkPassword($request->post('old_password'), $user);
            if (!$oldPassword) {
                throw new AdminException('原密码错误');
            }

            $user->password = $model->getPassword($request->post('password'), $user->getPasswordSalt());

            if ($user->save() === false) {
                throw new AdminException('修改失败');
            }

            return json(['code' => 0, 'message' => '修改成功', 'toUrl' => url('login/index')]);
        }
        return $this->render('change');
    }

    //退出系统
    public function logout()
    {
        $service = new Service();
        $service->logout();

        $this->redirect('admin/login');
    }

    //清除缓存
    public function clearCache()
    {
        $path = Env::get('app_path') . '../runtime/cache';
        $this->getAllClass($path);
        return json(['code' => 0, 'message' => '清除成功']);
    }

    private function getAllClass($path)
    {
        $dir = opendir($path);
        while (($file = readdir($dir)) !== false) {
            if ($file != '.' && $file != '..') {
                if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
                    $this->getAllClass($path . DIRECTORY_SEPARATOR . $file);
                    /*if (count(scandir($path . DIRECTORY_SEPARATOR . $file)) == 2) {//目录为空,=2是因为.和..存在
                        rmdir($path . DIRECTORY_SEPARATOR . $file);// 删除空目录
                    }*/
                } else {
                    @unlink($path . DIRECTORY_SEPARATOR . $file);
                }

            }
        }

        closedir($dir);
    }

    public function clear()
    {
        //清除所有session
        Session::destroy();
    }

}